<template>
  <div class="goodsinfo">
    <!-- swiper -->
    <div class="my-panel">
      <Swiper :lunbolist="lunbolist"></Swiper>
    </div>

    <van-panel
      class="my-panel"
      :title="goodsinfo.title"
    >
      <div class="content">
        <p class="price">
          <span class="new">现价：{{ goodsinfo.sell_price }}</span>
          <span class="old">原价：{{ goodsinfo.market_price }}</span>
        </p>
        <p class="num">购买数量：<van-stepper v-model="value" /></p>
      </div>
    </van-panel>

    <van-panel
      class="my-panel"
      title="商品信息"
    >
      <div class="content">
        <p>商品货号:{{ goodsinfo.goods_no }}</p>
        <p>库存情况:{{ goodsinfo.stock_quantity }}件</p>
        <p>上架时间:{{ goodsinfo.add_time | datefmt }}</p>
      </div>
    </van-panel>
    <Comment :id="id"></Comment>

    <van-goods-action>
      <van-goods-action-icon
        icon="chat-o"
        text="客服"
        dot
      />
      <van-goods-action-icon
        icon="cart-o"
        text="购物车"
        :badge="$store.getters['cart/getAllCount']"
        to="/cart"
      />
      <van-goods-action-icon
        icon="shop-o"
        text="店铺"
        badge="12"
      />
      <van-goods-action-button
        type="warning"
        text="加入购物车"
        @click="addCart"
      />
      <van-goods-action-button
        type="danger"
        text="立即购买"
      />
    </van-goods-action>
  </div>
</template>
<script>
import Swiper from '@/components/Swiper';
import Comment from '@/components/Comment';
export default {
  components: {
    Swiper,
    Comment,
  },
  props: ['id'],
  data() {
    return {
      goodsinfo: {},
      lunbolist: [],
      value: 1,
    };
  },
  created() {
    this.getGoodsInfo();
    this.getLunboList();
  },
  methods: {
    async getGoodsInfo() {
      const res = await this.$http.getGoodsInfo(this.id);
      this.goodsinfo = res.data.message;
    },
    async getLunboList() {
      const res = await this.$http.getThumImages(this.id);
      this.lunbolist = res.data.message;
    },
    addCart() {
      // 整合要加入的购物车的数据
      const goodsInfo = {
        id: this.id,
        src: this.lunbolist[0].src,
        price: this.goodsinfo.sell_price,
        count: this.value,
        selected: true,
        title: this.goodsinfo.title,
        timer: null,
      };
      // console.log(goodsInfo);
      // 通过mutations 修改state
      this.$store.commit('cart/ADD_CART', goodsInfo);
    },
  },
};
</script>
<style lang="scss">
.goodsinfo {
  padding: 10px;
  box-sizing: border-box;
  .my-swipe {
    height: 150px;
    text-align: center;
    img {
      height: 100%;
      width: 50%;
    }
  }
  .my-panel {
    border: 1px solid #eee;
    margin-bottom: 10px;
    .content {
      margin-left: 15px;
      font-size: 12px;
      .price {
        .new {
          color: red;
          margin-right: 20px;
        }
        .old {
          text-decoration: line-through;
        }
      }
      .num {
        display: flex;
        align-items: center;
      }
    }
  }
}
</style>
